package com.moleq.mgpos.activities;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;

import com.moleq.base.MoleQBaseActivity;
import com.moleq.db.DatabaseHelper;
import com.moleq.dialog.MessageBox;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.itemsetting.adapter.AppChooseAdapter;
import com.moleq.models.Functions;
import com.moleq.models.GlobalInfo;
import com.moleq.models.Users;
import com.moleq.mgpos.activities.R;
import com.moleq.service.FunctionService;
import com.moleq.service.ServiceManager;
import com.moleq.service.UsersService;

public class AppChooseActivity extends MoleQBaseActivity {

	private Context sharedContext = null;
//	private DBHelper dbHelper = null;
	private GridView gv_app_choose = null;
	private Button btn_app_choose_exit = null;
	//private MessageDialog mMessageDialog = null;
	private List<Functions> DataSource = null;
	private FunctionService functionService = null;
	private FunctionService inventoryfunctionService = null;
	private UsersService usersService = null;
	private AppChooseAdapter appChooseAdapter = null;
	private static final int POSRegister = 1;
	private static final int ItemSetting = 2;
	private static final int PriceSetting = 3;
	private static final int DepartmentSetting = 4;
	private static final int POSSetting = 5;
	private static final int UserSetting = 6;
	private static final int HardwareSetting = 7;
	private static final int FlipChartSetting = 8;
	private static final int MixMatchSetting = 9;
	private static final int Reports = 10;
	private static final int EJ = 11;
	private static final int DiscountSetting = 12;
	private static final int MemberSetting = 13;
	private static final int NetWorkSetting = 14;
	private static final int ClerkReport = 15;
	private static final int FunctionSetting = 16;
	private static final int NetworkReport = 17;
	private static final int TerminalReport = 18;
	private static final int ViewSignature = 19;
	private static final int Inventory = 20;
	private GlobalInfo mGlobalInfo;
	private int posSettingID;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_choose);
		functionService = ServiceManager.getInstance().mFunctionService;
		usersService = ServiceManager.getInstance().mUsersService;
		btn_app_choose_exit = (Button) findViewById(R.id.btn_app_choose_exit);
		btn_app_choose_exit.setOnClickListener(mOnClickListener);
		gv_app_choose = (GridView) findViewById(R.id.gv_app_choose);
		mGlobalInfo = GlobalInfo.getInstance();
		if (mGlobalInfo.cashier.equals("8888")) {
			DataSource = functionService.getfunctionlist();
		} else {
			DataSource = functionService.getuserfunctionlist(String
					.valueOf(usersService.FindUserId(mGlobalInfo.cashier)));
		}
		Users user = usersService.GetUserDetail(mGlobalInfo.cashier);
		appChooseAdapter = new AppChooseAdapter(this, DataSource,user);
		appChooseAdapter.setOnClickMessageListener(mOnClickAppChooseItem);
		gv_app_choose.setAdapter(appChooseAdapter);

		Drawable drawable = null;
		if (user.getUs_color() == 1 || mGlobalInfo.cashier.equals("8888")) {
			drawable = this.getResources().getDrawable(R.drawable.button_1);
		} else if (user.getUs_color() == 2) {
			drawable = this.getResources().getDrawable(R.drawable.button_2);
		} else if (user.getUs_color() == 3) {
			drawable = this.getResources().getDrawable(R.drawable.button_3);
		} else if (user.getUs_color() == 4) {
			drawable = this.getResources().getDrawable(R.drawable.button_4);
		} else if (user.getUs_color() == 5) {
			drawable = this.getResources().getDrawable(R.drawable.button_5);
		} else {
			drawable = this.getResources().getDrawable(R.drawable.button_1);
		}
		btn_app_choose_exit.setBackgroundDrawable(drawable);
	}

	public AppChooseAdapter.OnClickAppChooseItem mOnClickAppChooseItem = new AppChooseAdapter.OnClickAppChooseItem() {

		@Override
		public void onClickItem(int id) {
			// TODO Auto-generated method stub
			JumpToSetting(id);
		}

	};
	public OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_app_choose_exit:
//				ShowMessage(
//						getResources().getString(R.string.msg_exit_program),
//						MessageDialog.MESSAGE_BOX_YES_NO,
//						MessageDialog.ICON_INFO);
//				MessageBox.show(AppChooseActivity.this, getResources().getString(R.string.msg_exit_program).toString()
//						, onClickMessageListener,MessageDialog.MESSAGE_BOX_YES_NO);
				MessageBox.show(AppChooseActivity.this,AppChooseActivity.this.getResources().getString(R.string.msg_exit_program).toString()
						, mOnClickMessageListener, MessageDialog.MESSAGE_BOX_YES_NO);
				break;

			default:
				break;
			}
		}

	};

//	public void ShowMessage(String text, int type, int icon) {
//		// if(mMessageDialog.isShowing()) return;
//		if (mMessageDialog != null && mMessageDialog.isShowing()) {
//			mMessageDialog.dismiss();
//		}
//		mMessageDialog = new MessageDialog(AppChooseActivity.this);
//		mMessageDialog.setStyle(type);
//		mMessageDialog.setIcon(icon);
//		mMessageDialog.setMessage(text);
//		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
//		mMessageDialog.show();
//	}

//	public OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {
//
//		@Override
//		public void onClickYes() {
//			// TODO Auto-generated method stub
//			finish();
//		}
//
//		@Override
//		public void onClickNo() {
//			// TODO Auto-generated method stub
//			mMessageDialog.dismiss();
//		}
//
//		@Override
//		public void onClickOK() {
//			// TODO Auto-generated method stub
//
//		}
//
//	};

	OnClickMessageListener mOnClickMessageListener = new OnClickMessageListener() {
		
		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			finish();
		}
		
		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}
	};
	
	public void JumpToSetting(int id) {
		switch (id) {
		case POSRegister:
			startActivity(new Intent(AppChooseActivity.this,
					SaleActivity.class));
			break;
		case ItemSetting:
			startActivity(new Intent(AppChooseActivity.this,
					ItemSettingActivity.class));
			break;
		case PriceSetting:
			startActivity(new Intent(AppChooseActivity.this,
					PriceSettingActivity.class));
			break;
		case DepartmentSetting:
			startActivity(new Intent(AppChooseActivity.this,
					DepartmentSettingActivity.class));
			break;
		case POSSetting:
			startActivity(new Intent(AppChooseActivity.this,
					PosSettingActivity.class));
			break;
		case UserSetting:
			startActivity(new Intent(AppChooseActivity.this,
					UserSettingActivity.class));
			break;
		case HardwareSetting:
			startActivity(new Intent(AppChooseActivity.this,
					HardwareSettingActivity.class));
			break;
		case FlipChartSetting:
			startActivity(new Intent(AppChooseActivity.this,
					FlipChartSettingActivity.class));
			break;
		case MixMatchSetting:
			startActivity(new Intent(AppChooseActivity.this,
					MixmatchSettingActivity.class));
			break;
		case Reports:
			startActivity(new Intent(AppChooseActivity.this,
					ReportActivity.class));
			break;
		case EJ:
			startActivity(new Intent(AppChooseActivity.this,
					ElectronicJournalActivity.class));
			break;
		case DiscountSetting:
			startActivity(new Intent(AppChooseActivity.this,
					DiscountSettingActivity.class));
			break;
		case MemberSetting:
			startActivity(new Intent(AppChooseActivity.this,
					CustomerSettingActivity.class));
			break;
		case NetWorkSetting:
			startActivity(new Intent(AppChooseActivity.this,
					NetworkSettingActivity.class));
			break;
		case ClerkReport:
			startActivity(new Intent(AppChooseActivity.this,
					ClerkReportActivity.class));
			break;
		case FunctionSetting:
			startActivity(new Intent(AppChooseActivity.this,
					FunctionSettingActivity.class));
			break;
		case NetworkReport:
			if (ServiceManager.getInstance().mPosSettingService.getRowCount() == 0) {
				MessageBox.show(AppChooseActivity.this, String.format(getResources().getString(R.string.msg_no_postting), "(POS Setting)"), mNoPosSettingListener, MessageDialog.MESSAGE_BOX_OK);
				return;
			}
			boolean network = ServiceManager.getInstance().mPosSettingService.getAllPosSetting().get(0).isMGNetwork();
			posSettingID = ServiceManager.getInstance().mPosSettingService.getAllPosSetting().get(0).getPs_id();
			if (!network) {
				MessageBox.show(AppChooseActivity.this, "Do you want to turn ON network feature?", onDialogClickListener, MessageDialog.MESSAGE_BOX_YES_NO);
			}else {
				startActivity(new Intent(AppChooseActivity.this,
						NetworkReportActivity.class));
			}
			break;
		case TerminalReport:
			startActivity(new Intent(AppChooseActivity.this,
					TerminalReportActivity.class));
			break;
		case ViewSignature:
			startActivity(new Intent(AppChooseActivity.this,
					ViewSignatureActivity.class));
			break;
		case Inventory:
			inventoryfunctionService = ServiceManager.getInstance().mInventoryFunctionService;
			if (inventoryfunctionService.getfunctionlist().size() == 0) {
				MessageBox.show(AppChooseActivity.this, "This terminal does not setup for inventory");
				return;
			}else{
				startActivity(new Intent(AppChooseActivity.this,
						InventoryAppChooseActivity.class));
			}
			
			break;
		default:
			break;
		
			
		}
	}
	
	private OnClickMessageListener onDialogClickListener = new OnClickMessageListener() {

		public void onClickYes() {
			ServiceManager.getInstance().mPosSettingService.updatePosNetwork(posSettingID);
			startActivity(new Intent(AppChooseActivity.this,
					NetworkReportActivity.class));
		}

		public void onClickNo() {
			
		}

		public void onClickOK() {
		}
	};
	
	private OnClickMessageListener mNoPosSettingListener = new OnClickMessageListener() {

		public void onClickYes() {
		}

		public void onClickNo() {
		}

		public void onClickOK() {
			finish();
		}
	};
}
